;;;;=========================================================================
;;;;=========================================================================
;;;;
;;;; This file provides a manual version of the search and rescue client.
;;;;
;;;; DO NOT MODIFY THE CONTENTS OF THIS FILE -- it may be overwritten in 
;;;; later releases of the Search and Rescue simulator.
;;;;
;;;; Author: Eric Eaton
;;;;
;;;;=========================================================================
;;;;=========================================================================




(defun initialize ()
  (format t "Agent initialization:  done.~%"))





(defun process-result (score)
  (check-type score number "a number")
  (format t "Final Score: ~A~%" score))




  
(defun choose-action (perceptions &aux (hasValidAction nil) (hasValidDirection nil) (action nil) (direction nil) actionRequest)

  ;; display the current perceptions
  (format t "~%Perceptions: ~A~%" perceptions)
  (format t "~%Perceived Locations: ~A~%" (list-perceived-coordinates perceptions))
  (format t "~%Perceived Objects: ~A~%" (list-perceived-objects perceptions))
  
  ;; loop to get the action
  (loop until (equal hasValidAction t) do 
	;; display the prompt
	(format t "~%What is your next action?~%")
	(format t "Moves:   (N)orth   (E)ast     (S)outh    (W)est~%")
	(format t "Actions: (P)ickup  (D)ropoff  (C)Sense   (-)NoOp~%")
	
	;; read the user's selection
	(setf action (read))
	
	;; parse and process the user's selection
	(setf hasValidAction t)  ; temporarily assume the action is valid
	(cond ((string-equal action "N")
	       (setf action "MOVE")
	       (setf direction "NORTH"))
	      ((string-equal action "S")
	       (setf action "MOVE")
	       (setf direction "SOUTH"))
	      ((string-equal action "E")
	       (setf action "MOVE")
	       (setf direction "EAST"))
	      ((string-equal action "W")
	       (setf action "MOVE")
	       (setf direction "WEST"))
	      ((string-equal action "P")
	       (setf action "PICKUP"))
	      ((string-equal action "D")
	       (setf action "DROPOFF"))
	      ((string-equal action "C")
	       (setf action "SENSE"))
	      ((string-equal action "-")
	       (setf action "NOOP")
	       (setf direction ""))
	      (t            ; if the user didn't input a valid action, reverse our assumption
	       (format t "~%Invalid action.~%")  
	       (setf hasValidAction nil))))
  
  ;; loop to get the direction to go with the action, if needed
  (if (and (not (string-equal action "NOOP")) (not (string-equal action "MOVE")))
      (loop until (equal hasValidDirection t) do
	
	    ;; prompt for the direction
	    (format t "~%What is the direction for this ~A action?~%" action)
	    (format t " (N)orth  (E)ast    (S)outh    (W)est~%")
	    
	    ;; read the user's choice
	    (setf direction (read))
	    
	    ;; parse and process the choice
	    (setf hasValidDirection t)
	    (cond ((string-equal direction "N") (setf direction "NORTH"))
		  ((string-equal direction "S") (setf direction "SOUTH"))	
		  ((string-equal direction "E") (setf direction "EAST"))
		  ((string-equal direction "W") (setf direction "WEST"))
		  (t (setf hasValidDirection nil)
		     (format t "~%Error:  invalid direction.~%")))))
  
  ;; generate the action request
  (setf actionRequest (make-action
		       :name action
		       :direction direction))
  ;(setf actionRequest (concatenate 'string "<action> <name> "  action " </name> <direction> " direction " </direction> <time> " (write-to-string *current_time*) " </time> </action> "))

  ;; echo the action we're requesting
  (format t "~%Requesting action: ~A~%" actionRequest)
 
  ;; return the selected action string
  actionRequest)
